package cn.melon.mod.store.biz.impl;

import org.apache.log4j.Logger;
import org.json.JSONException;
import org.json.JSONObject;

import cn.melon.common.ModelType;
import cn.melon.helper.IOnRequestListener;
import cn.melon.helper.LoggerHelper;
import cn.melon.helper.RequestHelper;
import cn.melon.mod.store.biz.GoodsBiz;
import cn.melon.mod.store.entity.Goods;

public class GoodsBizImpl implements GoodsBiz {

    private String TAG = LoggerHelper.makeLogTag(GoodsBizImpl.class);
    private Logger goodsBizLog = LoggerHelper.clientLogger();
    
    /* (non-Javadoc)
     * @see cn.melon.mod.store.biz.GoodsBiz#addGoods(cn.melon.mod.store.entity.Goods)
     */
    @Override
    public boolean addGoods(Goods goods) {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "ADDGOODS");
            jsonReq.put("Goods", goods.toJsonObject());
            goodsBizLog.info(jsonReq);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.STORE).addRequest(jsonReq.toString())
                .addReceiver("addGoods", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        goodsBizLog.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {

                            } else {
                                // 错误处理
                                goodsBizLog.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
        return true;
    }

    /* (non-Javadoc)
     * @see cn.melon.mod.store.biz.GoodsBiz#removeGoodsById(java.lang.String)
     */
    @Override
    public boolean removeGoodsById(String GoodsId) {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "REMOVEGOODS");
            jsonReq.put("GoodsId", GoodsId);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.STORE).addRequest(jsonReq.toString())
                .addReceiver("removeGoods", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        goodsBizLog.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {

                            } else {
                                // 错误处理
                                goodsBizLog.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
        return true;
    }

    /* (non-Javadoc)
     * @see cn.melon.mod.store.biz.GoodsBiz#updateGoodsInfo(cn.melon.mod.store.entity.Goods)
     */
    @Override
    public boolean updateGoodsInfo(Goods goods) {
        JSONObject jsonReq = new JSONObject();
        try {
            jsonReq.put("action", "UPDATEGOODS");
            jsonReq.put("Goods", goods.toJsonObject());
        } catch (JSONException e) {
            e.printStackTrace();
        }

        new RequestHelper(ModelType.STORE).addRequest(jsonReq.toString())
                .addReceiver("updateGoods", new IOnRequestListener() {

                    @Override
                    public void onSuccess(String response) {
                        goodsBizLog.info(response);
                        try {
                            JSONObject jRet = new JSONObject(response);
                            if(jRet.getInt("code") == 200) {

                            } else {
                                // 错误处理
                                goodsBizLog.info(TAG + "ERROR");
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                        
                    }
                }).build().start();
        return true;
    }

}
